<?php

class Default_Model_AppointDetails extends Zend_Db_Table_Abstract
{
	private $db = null;
	protected $_name = 'appointment_details'; 
  	protected $_primary = 'appointment_id'; 
	public function init()
	{
		$this->db = Zend_Registry::get('DB');
	}
	public function addDetailsAppoint($data,$lastId)
	{		
		$startDate = new Zend_Date($data['start_time']);
		$endDate   = new Zend_Date($data['end_time']);
	
		$sqlData = array(
			'appointment_id' => $lastId,
			'services_id'    => $data['services'],
			'employee_id'    => $data['employee'],
			'start_time'     => $startDate->get('Y-M-d H:m:s'),
			'end_time'       => $endDate->get('Y-M-d H:m:s')
		);
		return $this->insert($sqlData);
	}
	public function delDetailsAppoint3Para($appId,$serviceId,$emId)
	{
		$where = $this->db->quoteInto('appointment_id = ?', $appId);
		$where .= "and ".$this->db->quoteInto('services_id = ?', $serviceId);
		$where .= "and ".$this->db->quoteInto('employee_id = ?', $emId);
		$this->delete($where);
		
		$select = $this->db->select();
	    $select->from('appointment_details')
	           ->where('appointment_id = ?',$appId);
		$data = $this->_db->query($select)->fetchAll();
		
		if (empty($data))
		{
			$appOb = new Default_Model_Appoint();
			$appOb->delApp($appId);
		}			   
	}
	public function delDetailsAppoint($id)
	{
		$where = $this->db->quoteInto('appointment_id = ?', $id);
		return $this->delete($where);
	}
	public function updateDetailsAppoint($id,$data)
	{
		$where = $this->db->quoteInto('appointment_id = ?', $id);
		return $this->update($data,$where);
	}
	public function getDetail($appointId)
	{
		$select = $this->db->select()
						   ->from(array('a'=>'appointment_details'))
						   ->joinInner(array('s'=>'services'),'s.services_id = a.services_id',array('services_name'))
						   ->joinInner(array('e'=>'employee'),'e.employee_id = a.employee_id',array('employee_name'))
						   ->where('appointment_id = ?', $appointId)
						   ->order(array('a.start_time','e.employee_name'));
		return $this->db->query($select)->fetchAll(Zend_Db::FETCH_ASSOC);				   
	}
	public function addAllDetail($data)
	{
		$lastId = $this->getAdapter()->lastInsertId("appointment");
		$data = $data['data'];
		$flag = false;
		foreach ($data as $item)
		{			
			$flag = $this->addDetailsAppoint($item,$lastId);
		}
		return $flag;
	}
	public function updateAllDetail($data,$id)
	{
		$this->delDetailsAppoint($id);
		$data = $data['data'];
		$flag = false;
		foreach ($data as $item)
		{			
			$flag = $this->addDetailsAppoint($item,$id);
		}
		return $flag;
	}
	public function getRowByEmployee($emp_id)
	{
		$select = $this->select('appointment_details')
				->where("employee_id = ?",$emp_id)	
			;
		return $this->_db->query($select)->fetchAll(Zend_Db::FETCH_ASSOC);
	}
        public function getCalenderbytime($date){
            $date = new Zend_Date($date,Zend_Date::DAY."-".Zend_Date::MONTH."-".Zend_Date::YEAR);
            $dateString = $date->get(Zend_Date::YEAR."-".Zend_Date::MONTH."-".Zend_Date::DAY);
            $sql = "select ad.appointment_id,c.customer_fname,c.customer_lname,s.services_name,e.employee_name,ad.start_time,ad.end_time
                from appointment_details ad inner join employee e on e.employee_id = ad.employee_id
					       inner join services s     on s.services_id = ad.services_id
					        inner join appointment a     on a.appointment_id = ad.appointment_id
					       inner join customer c     on a.customer_id = c.customer_id
                where start_time >= '$dateString  00:00:00'
                and end_time <= '$dateString  23:59:59'
                order by start_time asc
            ";
       
            return $this->_db->fetchAll($sql,Zend_Db::FETCH_ASSOC);
        }
}

?>